package com.hyfrogx.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.utils.ShiroUtils;
import com.hyfrogx.common.utils.SpringContextUtils;
import com.hyfrogx.modules.sys.form.SysLoginForm;
import com.hyfrogx.modules.sys.service.SysCaptchaService;
import com.hyfrogx.modules.sys.service.SysUserService;
import com.hyfrogx.modules.sys.service.SysUserTokenService;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

/**
 * TODO 登录相关操作
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/2
 */
@RestController
public class SysLoginController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	@Autowired
	private SysCaptchaService sysCaptchaService;

	/**
	 * 输出验证码
	 *
	 * @param response
	 * @param uuid
	 * @throws ServletException
	 * @throws IOException
	 */
	@GetMapping("captcha.jpg")
	public void captcha(HttpServletResponse response, String uuid) throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");

		//获取图片验证码
		BufferedImage image = sysCaptchaService.getCaptcha(uuid);

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}

	/**
	 * 用户登录
	 *
	 * @param form
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/sys/login")
	public Map<String, Object> login(@RequestBody SysLoginForm form) throws IOException {
		//开发环境屏蔽验证码
//		if (!StrUtil.equals(SpringContextUtils.getActiveProfile(), SpringContextUtils.DEV_PROFILE)) {
//			boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
//			if (!captcha) {
//				return R.error("验证码不正确");
//			}
//		}

		try {
			Subject subject = ShiroUtils.getSubject();
			AuthenticationToken authenticationToken = new UsernamePasswordToken(form.getUsername(), form.getPassword());
			subject.login(authenticationToken);
			return sysUserTokenService.createToken(ShiroUtils.getUserId());
		} catch (UnknownAccountException e) {
			throw new MyException("账号或密码不正确");
		} catch (LockedAccountException e) {
			throw new MyException("账号已被锁定,请联系管理员");
		} catch (Exception e) {
			throw e;
		}
	}


	/**
	 * 退出
	 */
	@PostMapping("/sys/logout")
	public R logout() {
		sysUserTokenService.logout(getUserId());
		return R.ok();
	}

}
